#include "ACE_Scheduler.h"

ACE_Scheduler::ACE_Scheduler( void )
{
    activate();
}

int ACE_Scheduler::svc( void )
{
    while(1){
        // Dequeue the next method object
        auto_ptr<ACE_Method_Request> request(activation_queue_.dequeue());

        //ACE_DEBUG((LM_DEBUG,"\n(%P|%t) ACE_Scheduler::dequeue,queue size [%d]\n",activation_queue_.method_count()));

        // Invoke the method request.
        if (request->call () == -1)
            break;
    }

    return 0x5678;
}

int ACE_Scheduler::enqueue( ACE_Method_Request *request,long prio )
{
    //ACE_DEBUG((LM_DEBUG,"\n(%P|%t) ACE_Scheduler::enqueue,queue size [%d]\n",activation_queue_.method_count()+1));

    request->priority(prio);
    return activation_queue_.enqueue (request);
}

void ACE_Scheduler::exit( void )
{
    class ExitMethod : public ACE_Method_Request
    {
    public:
        virtual int call (void){
            //ACE_DEBUG((LM_DEBUG,"\n(%P|%t) ACE_Scheduler::exit.\n"));
            return -1;  // Cause exit.
        }
    };

    enqueue(new ExitMethod);
    wait();
}